﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.GameService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 游戏设置视图模型
    /// </summary>
    public class GameSettingViewModel : SubPageFrameViewModelBase
    {
        public GameSettingViewModel()
            : base(PageCategory.系统设置.ToString(),Page.游戏设置.ToString())
        {
            SubmitCommand = new UniversalCommand(Submit);
            RefreshCommand = new UniversalCommand(Refresh);
            Refresh(null);
        }

        string eachChapterRoundCount;
        string eachRoundTimeSpan;
        string eachChapterTimeSpan;
        string limitRedGolds;
        string closureSingleTime;

        public UniversalCommand SubmitCommand { get; set; }

        public UniversalCommand RefreshCommand { set; get; }

        public void Submit(object param)
        {

            this.ComfirmMsg("确认修改吗?",()=>{
                try
                {
                    GameServiceClient serv = new GameServiceClient();
                    serv.EditGameSettingCompleted += serv_EditGameSettingCompleted;
                    serv.EditGameSettingAsync(new EditSettingOfGameImport()
                    {
                        Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token,
                        EachChapterRoundCount = int.Parse(this.EachChapterRoundCount),
                        EachRoundTimeSpan = int.Parse(this.EachRoundTimeSpan),
                        EachChapterTimeSpan = int.Parse(this.EachChapterTimeSpan),
                        LimitRedGolds = double.Parse(this.LimitRedGolds),
                        ClosureSingleTime = int.Parse(this.ClosureSingleTime)
                    });
                }
                catch
                {
                    this.ShowError("请输入正确的数字");
                }
            });
          
      
        }

        void serv_EditGameSettingCompleted(object sender, EditGameSettingCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.ShowMsg("修改成功");
            }
            else
            {
                this.ShowError(e.Result.Error);
            }
        }

        public void Refresh(object param)
        {
            GameServiceClient serv = new GameServiceClient();
            serv.GetGameSettingCompleted += this.WrapEventHandler<GetGameSettingCompletedEventArgs>(serv_GetGameSettingCompleted);
            serv.GetGameSettingAsync(new GetSettingOfGameImport() 
            { Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token });
        }

        void serv_GetGameSettingCompleted(object sender, GetGameSettingCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
               GameSettingExport setting =  e.Result.Info;
               this.EachChapterRoundCount = setting.EachChapterRoundCount.ToString();
               this.EachRoundTimeSpan = setting.EachRoundTimeSpan.ToString();
               this.EachChapterTimeSpan = setting.EachChapterTimeSpan.ToString();
               this.LimitRedGolds = setting.LimitRedGolds.ToString();
               this.ClosureSingleTime = setting.ClosureSingleTime.ToString();
            }else
            {
                this.ShowError(e.Result.Error);
            }
        }

        /// <summary>
        /// 每轮的局数,默认100局
        /// </summary>
        public string EachChapterRoundCount
        {
            get
            {
                return eachChapterRoundCount;
            }
            set
            {
                if (eachChapterRoundCount != value)
                {
                    eachChapterRoundCount = value;
                    OnPropertyChanged("EachChapterRoundCount");
                }
            }
        }

        /// <summary>
        /// 每局游戏间隔时间(秒),默认40秒
        /// </summary>
        public string EachRoundTimeSpan
        {
            get
            {
                return eachRoundTimeSpan;
            }
            set
            {
                if (eachRoundTimeSpan != value)
                {
                    eachRoundTimeSpan = value;
                    OnPropertyChanged("EachRoundTimeSpan");
                }
            }
        }
        /// <summary>
        /// 每轮游戏间隔时间(秒),默认2分钟
        /// </summary>

        public string EachChapterTimeSpan
        {
             get
            {
                return eachChapterTimeSpan;
            }
            set
            {
                if ( eachChapterTimeSpan!= value)
                {
                    eachChapterTimeSpan = value;
                    OnPropertyChanged("EachChapterTimeSpan");
                }
            }
        }

        /// <summary>
        /// 限红分数
        /// </summary>
        public string LimitRedGolds
        {
             get
            {
                return limitRedGolds;
            }
            set
            {
                if (limitRedGolds != value)
                {
                    limitRedGolds = value;
                    OnPropertyChanged("LimitRedGolds");
                }
            }
        }

        /// <summary>
        /// 封单时间（秒）
        /// </summary>
        public string ClosureSingleTime
        {
             get
            {
                return closureSingleTime;
            }
            set
            {
                if (closureSingleTime != value)
                {
                    closureSingleTime = value;
                    OnPropertyChanged("ClosureSingleTime");
                }
            }
        }
    }
}
